package org.su.mamamiya.swagger.config;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.Collections;

@Slf4j
@Configuration
@EnableConfigurationProperties(SwaggerConfig.class)
@EnableSwagger2
public class SwaggerAutoConfig {

    @Autowired
    private SwaggerConfig swaggerConfig;

    @Bean
    @ConditionalOnMissingBean
    public Docket createRestApi() {
        log.warn("....Swagger start build:{}......", ToStringBuilder.reflectionToString(swaggerConfig));
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerConfig.enable)
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerConfig.getBasePackage()))
                .paths(PathSelectors.any())
                .build().securitySchemes(Collections.singletonList(securitySchemes()))
                .securityContexts(Collections.singletonList(securityContexts()));

    }

    /**
     * 认证方式使用密码模式
     */

    private SecurityScheme securitySchemes() {

        GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("/dev/oauth/token");

        return new OAuthBuilder()
                .name("Authorization")
                .grantTypes(Collections.singletonList(grantType))
                .scopes(Arrays.asList(scopes()))
                .build();

    }

    /**
     * 设置 swagger2 认证的安全上下文
     */

    private SecurityContext securityContexts() {

        return SecurityContext.builder()
                .securityReferences(Collections.singletonList(new SecurityReference("Authorization", scopes())))
                .forPaths(PathSelectors.any())
                .build();

    }

    /**
     * 允许认证的scope
     */

    private AuthorizationScope[] scopes() {
        AuthorizationScope authorizationScope = new AuthorizationScope("test", "接口测试");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return authorizationScopes;
    }


    /**
     * 文档标题
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(swaggerConfig.getTitle())
                .description(swaggerConfig.getDescription())
                // .termsOfServiceUrl("http://blog.csdn.net/saytime")
                .version(swaggerConfig.getVersion())
                .build();
    }
}
